#include "StdAfx.h"
#include "CircuitNetLabel.h"


CCircuitNetLabel::CCircuitNetLabel(void)
{
	m_dblPosX = 0.0;
	m_dblPosY = 0.0;
	m_rotateAngle = 0.0;
	m_dblColor[0] = 0.0;
	m_dblColor[1] = 0.0;
	m_dblColor[2] = 0.0;
	m_pFont = NULL;
	m_bSelected = false;
	m_szLabel = _T("");
	m_justification = -1;
}

CCircuitNetLabel::CCircuitNetLabel(const CCircuitNetLabel *pEntityObj)
	: CCircuitUnit(pEntityObj)
{
	m_dblPosX = pEntityObj->GetPosX();
	m_dblPosY = pEntityObj->GetPosY();
	m_rotateAngle = pEntityObj->GetRotateAngle();
	m_dblColor[0] = pEntityObj->GetColorR();
	m_dblColor[1] = pEntityObj->GetColorG();
	m_dblColor[2] = pEntityObj->GetColorB();
	m_pFont =NULL;
	if (pEntityObj->GetFont())
	{
		m_pFont = pEntityObj->GetFont()->Clone();
	}
	m_bSelected = pEntityObj->GetIsSelected();
	m_szLabel = pEntityObj->GetLable();
	m_justification = pEntityObj->GetJustification();
}

CCircuitNetLabel::CCircuitNetLabel(CICircuit *pParentEnt)
	:CCircuitUnit(pParentEnt)
{
	m_dblPosX = 0.0;
	m_dblPosY = 0.0;
	m_rotateAngle = 0.0;
	m_dblColor[0] = 0.0;
	m_dblColor[1] = 0.0;
	m_dblColor[2] = 0.0;
	m_pFont = NULL;
	m_bSelected = false;
	m_szLabel = _T("");
	m_justification = -1;
}

CCircuitNetLabel::~CCircuitNetLabel(void)
{
	Clear();
}

void CCircuitNetLabel::Serialize(CArchive & ar)
{
	if (ar.IsStoring())
	{
		ar << m_dblPosX;
		ar << m_dblPosY;
		ar << m_rotateAngle;
		ar << m_dblColor[0];
		ar << m_dblColor[1];
		ar << m_dblColor[2];
		m_pFont->Serialize(ar);
		ar << m_bSelected;
		ar << m_justification;
		CObj::WriteString(ar, m_szLabel);
	}
	else
	{
		ar >> m_dblPosX;
		ar >> m_dblPosY;
		ar >> m_rotateAngle;
		ar >> m_dblColor[0];
		ar >> m_dblColor[1];
		ar >> m_dblColor[2];
		m_pFont = new CCircuitFont;
		m_pFont->Serialize(ar);
		ar >> m_bSelected;
		if( CObj::m_lFileVersion > 40 )
			ar >> m_justification;
		CObj::ReadString(ar, m_szLabel);
	}
	CCircuitUnit::Serialize(ar);
}

CObj *CCircuitNetLabel::Clone() const
{
	return new CCircuitNetLabel(this);
}

void CCircuitNetLabel::Transform(const CMatrix transf)
{

}

bool CCircuitNetLabel::GetBox(double & dblLeft, double & dblBottom, double & dblRight, double & dblTop) const
{
	return false;
}

void CCircuitNetLabel::Clear()
{
	delete m_pFont;
}

void CCircuitNetLabel::CopyDisplayObj(long lSourceLayerId, long lDestLayerId)
{
	CEntityObj::CopyDisplayObj(lSourceLayerId, lDestLayerId);
}

void CCircuitNetLabel::DeleteDisplayObj(long lLayerId)
{
	CEntityObj::DeleteDisplayObj(lLayerId);
}

CDisplayObj *CCircuitNetLabel::CreateDisplayObj(long lLayerId)
{
	CCircuitNetLabelDisObj *pLabelDis = new CCircuitNetLabelDisObj(this);
	pLabelDis->SetLayerId(lLayerId);
	return pLabelDis;
}

void CCircuitNetLabel::CalcBox()
{
	if (m_pFont)
	{
		int len = m_szLabel.GetLength();
		double dblTextHeight = m_pFont->GetSize();
		double dblTextWidth = dblTextHeight*len;
		m_minX = m_dblPosX - 0.5*dblTextWidth;
		m_minY = m_dblPosY - 0.5*dblTextHeight;
		m_maxX = m_dblPosX + 0.5*dblTextWidth;
		m_maxY = m_dblPosY + 0.5*dblTextHeight;
	}
}

eCircuitUnitEntType CCircuitNetLabel::GetUnitType() const
{
	return CIRCUIT_NETLABEL;
}

double CCircuitNetLabel::GetPosX() const
{
	return m_dblPosX;
}
double CCircuitNetLabel::GetPosY() const
{
	return m_dblPosY;
}

double CCircuitNetLabel::GetRotateAngle() const
{
	return m_rotateAngle;
}

double CCircuitNetLabel::GetColorR() const
{
	return m_dblColor[0];
}

double CCircuitNetLabel::GetColorG() const
{
	return m_dblColor[1];
}

double CCircuitNetLabel::GetColorB() const
{
	return m_dblColor[2];
}

CCircuitFont * CCircuitNetLabel::GetFont() const
{
	return m_pFont;
}

bool CCircuitNetLabel::GetIsSelected() const
{
	return m_bSelected;
}

CString CCircuitNetLabel::GetLable() const
{
	return m_szLabel;
}

void CCircuitNetLabel::SetPosX(double pos) 
{
	m_dblPosX = pos;
}

void CCircuitNetLabel::SetPosY(double pos) 
{
	m_dblPosY = pos;
}

void CCircuitNetLabel::SetRotateAngle(double rotateAngle) 
{
	m_rotateAngle = rotateAngle;
}

void CCircuitNetLabel::SetColorR(double colValue) 
{
	m_dblColor[0] = colValue;
}

void CCircuitNetLabel::SetColorG(double colValue) 
{
	m_dblColor[1] = colValue;
}

void CCircuitNetLabel::SetColorB(double colValue) 
{
	m_dblColor[2] = colValue;
}

void CCircuitNetLabel::SetFont(CCircuitFont *pFont) 
{
	m_pFont = pFont;
}

void CCircuitNetLabel::SetIsSelected(bool bFlag) 
{
	m_bSelected = bFlag;
}

void CCircuitNetLabel::SetLable(CString szLabel) 
{
	m_szLabel = szLabel;
}

void CCircuitNetLabel::SetJustification(int val)
{
	m_justification = val;
}

int CCircuitNetLabel::GetJustification() const
{
	return m_justification;
}